package com.huawei.M01.iterator.M24;

import java.util.*;

/**
 * 1032. 字符流
 *
 * @author 111
 */
public class StreamChecker {
    private final Set<String> words = new HashSet<>();

    private final StringBuilder sb = new StringBuilder();


    public StreamChecker(String[] words) {
        for (String word : words) {
            if (words == null || word.trim().length() == 0) {
                continue;
            }
            this.words.add(word);
        }
    }

    public boolean query(char letter) {
        sb.append(letter);
        return hasWord();
    }

    private boolean hasWord() {
        for (String word : words) {
            if (sb.toString().endsWith(word)) {
                return true;
            }
        }
        return false;
    }
}
